'use strict'

define(['jquery', 'bootstrap', 'select2', 'datetimepicker', 'datetimepickerCN',
        'shiftform', 'validate', 'validate.en'], function($) {
    return {
        init: function() {
            this.initPage();
            this.initRefund();
            this.initChangeSalesman();
            this.initInvoice();
            this.initPay();
            this.initDistributionChannel();
            this.initDeposit();
            this.initBalancePayment();
            this.initOffer();
            this.initEdit();
            this.initShip();
            this.initClose();
        },
        
        initPage: function(){
            $('select[name="siteId"]').select2({
                minimumResultsForSearch : -1,
                allowClear : true,
                width: '150'
            });
            
            $('select[name="paymentStatus"]').select2({
                placeholder : '请选择支付状态',
                minimumResultsForSearch : -1,
                allowClear : true,
                width: '100%'
            });
            
            $('a.x-btn').click(function(){
                $("#orderType").val($(this).attr("id"));
                $("#orders-search-form").submit();
            });
            
            $('#sub_btn').click(function(){
                $("#orderType").val('');
                $("#orders-search-form").submit();
            });
        },
        
        initRefund: function(){
            $(document).on("shown.bs.modal", "#refund", function(e) {
                var $form = $(e.currentTarget).find("form");
                $form.bt_validate();
                $form.shiftform({
                    beforeSubmit: function(el) {
                        $('form').trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                });
            })
        },
        
        initChangeSalesman: function(){
            $(document).on("shown.bs.modal", "#change-salesman", function(e) {
                var $form = $(e.currentTarget).find("form");
                $form.bt_validate();
                $form.shiftform({
                    beforeSubmit: function(el) {
                        $('form').trigger("validate.ajax.submit");return $.bt_validate.result[$.bt_validate.form_id]
                    }
                });
            })
        },
        
        initInvoice: function(){
            $(document).on("shown.bs.modal", "#invoice", function(e) {
                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            })
        },
        
        initPay: function(){
            $(document).on("shown.bs.modal", "#pay", function(e) {
                var $form = $(e.currentTarget).find("form");
                $form.bt_validate();
                $form.shiftform({
                    beforeSubmit: function(el) {
                        $('form').trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                });
            })
        },
        
        initDistributionChannel: function(){
            $(document).on("shown.bs.modal", "#distribution-channel", function(e) {
                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            })
        },
        
        initDeposit: function(){
            $(document).on("shown.bs.modal", "#deposit", function(e) {
                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            })
        },
        
        initBalancePayment: function(){
            $(document).on("shown.bs.modal", "#balance-payment", function(e) {
                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            })
        },
        
        initOffer: function(){
            $(document).on("shown.bs.modal", "#offer", function(e) {
                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            })
        },
        
        initEdit: function(){
            $(document).on("shown.bs.modal", "#remark", function(e) {
                var $form = $(e.currentTarget).find("form");
                $form.bt_validate();
                $form.shiftform({
                    beforeSubmit: function(el) {
                        $('form').trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                });
            })
        },
        
        initShip: function(){
            $(document).on("shown.bs.modal", "#ship", function(e) {
                var $form = $(e.currentTarget).find("form");
                $form.bt_validate();
                $form.shiftform({
                    beforeSubmit: function(el) {
                        $('form').trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                });
            })
        },
        
        initClose: function(){
            $(document).on("shown.bs.modal", "#close", function(e) {
                $(e.currentTarget).find("form").shiftform();
            })
        }
    }
})
